package com.zhou.jiancai.fragment;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zhou.jiancai.Interface.MovieService;
import com.zhou.jiancai.R;
import com.zhou.jiancai.Utils.LogUtils;
import com.zhou.jiancai.Utils.NetWorkUtils;
import com.zhou.jiancai.activity.NiceViewShowActivity;
import com.zhou.jiancai.response.ResponseData;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/28.
 */

public class MeFragment extends Fragment implements View.OnClickListener {

    @InjectView(R.id.lv)
    ListView lv;
    private ArrayList<ResponseData.DataBean.MoviesBean> movieBean;
    private TextView textViewError;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, null);
        textViewError = (TextView) view.findViewById(R.id.tv_me_error);
        ButterKnife.inject(this, view);
        textViewError.setVisibility(View.VISIBLE);
        textViewError.setText("正在加载...");

        getMovie();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getMovie();
    }

    private void getMovie() {
        //是否连接wifi
        if (NetWorkUtils.isNetConnected(getActivity())) {
            Toast.makeText(getActivity(),"网络可用",Toast.LENGTH_SHORT).show();
            getromoteData();
        }
        //是否连接4g
        if (NetWorkUtils.isNetworkAvailable(getActivity())) {
           // getromoteData();
            //Toast.makeText(getActivity(),"网络可用",Toast.LENGTH_SHORT).show();

        }else {
            Toast.makeText(getActivity(),"请检查网络",Toast.LENGTH_SHORT).show();
        }




    }

    private void getromoteData() {
        String baseUrl = "http://m.maoyan.com/movie/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MovieService movieservice = retrofit.create(MovieService.class);
        Call<ResponseData> call = movieservice.getTopMovie();
        call.enqueue(new Callback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {

                LogUtils.d(response.body().toString());
           /* Toast.makeText(getActivity(), "网络请求成功了", Toast.LENGTH_SHORT).show();
            Gson gson = new Gson();
            ResponseData responseData = gson.fromJson(response.body().toString(), ResponseData.class);
            movieBean = responseData.data.movies;//在setAdapter之前给数组赋值
            MyAdapter myadapter = new MyAdapter();

            lv.setAdapter(myadapter);*/
                SystemClock.sleep(1000);//模拟延迟
                textViewError.setVisibility(View.INVISIBLE);
                movieBean = response.body().data.movies;//在setAdapter之前给数组赋值
                MyAdapter myadapter = new MyAdapter();

                lv.setAdapter(myadapter);
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                textViewError.setVisibility(View.VISIBLE);
                textViewError.setText(t.toString());

            }
        });
    }

    public class MyAdapter extends BaseAdapter {


        private MeFragment.viewHolder viewHolder;
        private Button btnTop;
        private Button btnUnRead;
        private Button btnDelete;
        private LinearLayout linearLayout;

        @Override
        public int getCount() {
            return movieBean.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = parent.inflate(getActivity(), R.layout.fragment_person_item_swip, null);
                linearLayout = (LinearLayout) view.findViewById(R.id.ll_item_swipe_left);
                TextView tv_desc = (TextView) view.findViewById(R.id.tv_desc);
                TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
                TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
                btnTop = (Button) view.findViewById(R.id.btnTop);
                btnUnRead = (Button) view.findViewById(R.id.btnUnRead);
                btnDelete = (Button) view.findViewById(R.id.btnDelete);
                ImageView imageView = (ImageView) view.findViewById(R.id.iv);
                viewHolder = new viewHolder(tv_desc, tv_time, tv_name, imageView);
                view.setTag(viewHolder);


            } else {
                view = convertView;
              viewHolder= (MeFragment.viewHolder) view.getTag();//给view设置标记  并将它付给holder


            }

            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "您点击了"+position, Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(getActivity(),NiceViewShowActivity.class);
                    startActivity(intent);
                }
            });
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "点击了删除按钮", Toast.LENGTH_SHORT).show();
                }
            });
            btnUnRead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "点击了未读按钮", Toast.LENGTH_SHORT).show();
                }
            });
            btnTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "点击了置顶按钮", Toast.LENGTH_SHORT).show();
                }
            });


            viewHolder.tv_desc.setText(movieBean.get(position).showInfo);
            viewHolder.tv_name.setText(movieBean.get(position).nm);
            viewHolder.tv_time.setText(movieBean.get(position).showDate);
            if (movieBean.get(position).img == null) {
                viewHolder.iv.setImageResource(R.mipmap.ic_launcher);


            } else {

                Glide.with(getActivity())
                        .load(movieBean.get(position).img)
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                viewHolder.iv.setImageBitmap(resource);
                            }
                        });


            }

            return view;
        }
    }

    class viewHolder {
        public viewHolder(TextView tv_desc, TextView tv_time, TextView tv_name, ImageView iv) {
            this.tv_desc = tv_desc;
            this.tv_time = tv_time;
            this.tv_name = tv_name;
            this.iv = iv;
        }

        TextView tv_desc;
        TextView tv_time;
        TextView tv_name;
        ImageView iv;

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
      //  OkGo.getInstance().cancelAll();
    }
}
